/*
 * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
 * All rights reserved.
 * [See end of file]
 */

package arq.examples;

import com.hp.hpl.jena.query.ARQ;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.larq.IndexBuilderString;
import com.hp.hpl.jena.query.larq.IndexLARQ;
import com.hp.hpl.jena.query.larq.LARQ;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.util.Utils;
import com.hp.hpl.jena.util.FileManager;

/**
 * Example code to load a model from a file, index all string literals, then
 * execute a SPARQL query with a Lucene search in it.
 * 
 * @author Andy Seaborne
 */

public class ExLucene1 {

	public static void main(String[] a) throws Exception {
		System.out.println("ARQ Example: "
				+ Utils.classShortName(ExLucene1.class));
		System.out.println("ARQ: " + ARQ.VERSION);
		System.out.println();

		Model model = ModelFactory.createDefaultModel();
		IndexLARQ index = buildIndex(model, "data/owl/TWI3.owl");

		// Search for
		String searchString = "+document";

		// String queryString = StringUtils.join("\n", new String[]{
		// "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" ,
		// "PREFIX : <http://example/>" ,
		// "PREFIX pf: <http://jena.hpl.hp.com/ARQ/property#>",
		// "SELECT * {" ,
		// " ?lit pf:textMatch '"+searchString+"'.",
		// "}"
		// }) ;
		String queryString = "SELECT ?subject ?object WHERE { ?subject rdfs:subClassOf ?object }";

		// Two of three docuemnts should match.
		performQuery(model, index, queryString);
		index.close();
	}

	static IndexLARQ buildIndex(Model model, String datafile) {
		// ---- Read and index all literal strings.
		IndexBuilderString larqBuilder = new IndexBuilderString();

		// Index statements as they are added to the model.
		model.register(larqBuilder);

		// To just build the index, create a model that does not store
		// statements
		// Model model2 = ModelFactory.createModelForGraph(new GraphSink()) ;

		FileManager.get().readModel(model, datafile);

		// ---- Alternatively build the index after the model has been created.
		// larqBuilder.indexStatements(model.listStatements()) ;

		// ---- Finish indexing
		larqBuilder.closeWriter();
		model.unregister(larqBuilder);

		// ---- Create the access index
		IndexLARQ index = larqBuilder.getIndex();
		return index;
	}

	static void performQuery(Model model, IndexLARQ index, String queryString) {
		// Make globally available
		LARQ.setDefaultIndex(index);

		Query query = QueryFactory.create(queryString);
		query.serialize(System.out);
		System.out.println();

		QueryExecution qExec = QueryExecutionFactory.create(query, model);
		// LARQ.setDefaultIndex(qExec.getContext(), index) ;
		ResultSet rs = qExec.execSelect();

		while (rs.hasNext()) {
			Object next = rs.next();
			System.out.println(next);
		}
		ResultSetFormatter.out(System.out, rs, query);
		qExec.close();
	}

}

/*
 * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP All
 * rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer. 2. Redistributions in
 * binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution. 3. The name of the author may not
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */